package com.kamistoat.meimeistore.modulessystem.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.kamistoat.meimeistore.apisystem.domain.SysOperLog;
import com.kamistoat.meimeistore.apisystem.entity.SysOperLogEntity;
import com.kamistoat.meimeistore.commoncore.utils.poi.ExcelUtil;
import com.kamistoat.meimeistore.commoncore.web.controller.BaseController;
import com.kamistoat.meimeistore.commoncore.web.domain.AjaxResult;
import com.kamistoat.meimeistore.commoncore.web.page.TableDataInfo;
import com.kamistoat.meimeistore.commonlog.annotation.Log;
import com.kamistoat.meimeistore.commonlog.enums.BusinessType;
import com.kamistoat.meimeistore.commonsecurity.annotation.InnerAuth;
import com.kamistoat.meimeistore.commonsecurity.annotation.RequiresPermissions;
import com.kamistoat.meimeistore.modulessystem.service.SysOperLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;


/**
 * 操作日志记录
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2022-08-09 10:34:46
 */
@RestController
@RequestMapping("/operlog")
public class SysOperLogController extends BaseController {
    @Autowired
    private SysOperLogService sysOperLogService;


    @RequiresPermissions("system:operlog:list")
    @GetMapping("/list")
    public TableDataInfo list(SysOperLog operLog) {
        startPage();
        List<SysOperLogEntity> sysOperLogEntityList = sysOperLogService.selectOperLogList(operLog);
        return getDataTable(entity2Vo(sysOperLogEntityList));
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:operlog:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLog operLog) {
        List<SysOperLogEntity> sysOperLogEntityList = sysOperLogService.selectOperLogList(operLog);
        ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
        util.exportExcel(response, entity2Vo(sysOperLogEntityList), "操作日志");
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:operlog:remove")
    @DeleteMapping("/{operIds}")
    public AjaxResult remove(@PathVariable Long[] operIds) {
        return toAjax(sysOperLogService.removeByIds(Arrays.asList(operIds)));
    }

    @RequiresPermissions("system:operlog:remove")
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        sysOperLogService.cleanOperLog();
        return AjaxResult.success();
    }

    @InnerAuth
    @PostMapping
    public AjaxResult add(@RequestBody SysOperLog operLog) {
        return toAjax(sysOperLogService.save(vo2Entity(operLog)));
    }


    /**
     * Entity 和 Vo类的转换
     */
    private List<SysOperLog> entity2Vo(List<SysOperLogEntity> sysOperLogEntityList) {
        return
                sysOperLogEntityList.stream().map(sysOperLogEntity -> {
                    SysOperLog sysOperLog = new SysOperLog();
                    BeanUtils.copyProperties(sysOperLogEntity, sysOperLog);
                    return sysOperLog;
                }).collect(Collectors.toList());
    }

    private SysOperLog entity2Vo(SysOperLogEntity sysOperLogEntity) {
        SysOperLog sysOperLog = new SysOperLog();
        BeanUtils.copyProperties(sysOperLogEntity, sysOperLog);
        return sysOperLog;
    }

    private List<SysOperLogEntity> vo2Entity(List<SysOperLog> sysOperLogList) {
        return
                sysOperLogList.stream().map(sysOperLog -> {
                    SysOperLogEntity sysOperLogEntity = new SysOperLogEntity();
                    BeanUtils.copyProperties(sysOperLog, sysOperLogEntity);
                    return sysOperLogEntity;
                }).collect(Collectors.toList());
    }

    private SysOperLogEntity vo2Entity(SysOperLog sysOperLog) {
        SysOperLogEntity sysOperLogEntity = new SysOperLogEntity();
        BeanUtils.copyProperties(sysOperLog, sysOperLogEntity);
        return sysOperLogEntity;
    }

}
